package com.jeehentor.ai.config;


import org.springframework.stereotype.Component;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description :  简易版会话上下文缓存（基于内存）
 * @Author : wzd
 * @Date : 2025/4/17 15:47
 * @Version : 1.0
 **/

@Component
public class AiSessionManager {

    private final Map<String, List<Map<String, String>>> sessionCache = new ConcurrentHashMap<>();

    /**
     * 获取当前会话的所有历史 messages
     */
    public List<Map<String, String>> getHistory(String sessionId) {
        return sessionCache.getOrDefault(sessionId, new ArrayList<>());
    }

    /**
     * 追加 user + assistant 消息
     */
    public void appendMessage(String sessionId, String role, String content) {
        sessionCache.computeIfAbsent(sessionId, k -> new ArrayList<>())
                .add(Map.of("role", role, "content", content));
    }

    /**
     * 清空某个会话
     */
    public void clearSession(String sessionId) {
        sessionCache.remove(sessionId);
    }
}